jQuery.each(["get", "post", "options"], function (i, method) {
    jQuery[method] = function (url, data, callback, type) {
        if (jQuery.isFunction(data)) {
            type = type || callback;
            callback = data;
            data = undefined;
        }
        return jQuery.ajax({
            url: url,
            type: method,
            dataType: 'json',
            contentType: 'application/json',
            data: data,
            success: callback,
            beforeSend: function (request) {
                request.setRequestHeader("sessionid", sessionStorage.getItem("tokenid"));
            }
        });
    };
});

// 共同的请求前缀
var prexurl = "http://127.0.0.1:9004/";

/**
 *共同请求后台方法 
 */
var sendrequest = function(url, type, params, calLback) {
	var successcalLback = function(data, textStatus, request) {
		if (data.code == 501) {
			sessionStorage.clear();
			window.location.href = 'login.html';
		} else {
            var sessionId = request.getResponseHeader("sessionid");
			// 刷新token
			sessionStorage.setItem('sessionid', sessionId);
			// 执行回调函数
			calLback();
		}
	}
	$.ajax({
		type: type,
		url: prexurl + url,
		contentType: "application/json; charset=utf-8",
		data: params,
		beforeSend: function(request) {
			request.setRequestHeader("sessionid", sessionStorage.getItem("tokenid"));
		},
		success: successcalLback,
		error: function() {
            $.iMessager.alert("服务器内部错误，请联系管理员");
		}
	});
}

/**
 *请求后台数据
 */
var ajaxget = function(url, params, calLback) {
	var successcalLback = function(data, textStatus, jqXHR) {
		if (data.code == 501) {
			sessionStorage.clear();
			window.location.href = 'login.html';
		} else {
            var sessionId = jqXHR.getResponseHeader("sessionid");
            // 刷新token
            sessionStorage.setItem('sessionid', sessionId);
			// 执行回调函数
			calLback(data);
		}
	}
	$.get(prexurl + url, params, successcalLback, "json");
}